package com.lc.recovery.ui.activity.warehousemine;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.StorageOutListApi;
import com.lc.recovery.http.response.StorageOutBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.SPUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OutboundRecordActivity extends AppActivity {
    @BindView(R.id.tv_year)
    TextView tvYear;
    @BindView(R.id.tv_month)
    TextView tvMonth;
    @BindView(R.id.rv_outboundrecord)
    RecyclerView rvOutboundrecord;
    @BindView(R.id.rl_all)
    RelativeLayout rlAll;
    @BindView(R.id.layout_refresh)
    TwinklingRefreshLayout layoutRefresh;
    private List<StorageOutBean.DataBean> dataBeanList = new ArrayList<>();
    private BaseQuickAdapter<StorageOutBean.DataBean, BaseViewHolder> outboundrecordadapter;
    //时间选择器
    private TimePickerView pvTime;
    private int year;
    private int month;
    private int day_of_month;
    private int hour;
    private int minute;
    private Calendar startDate;
    private Calendar endDate;
    private String dateNowStr;
    private int total;
    private int page = 1;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_outboundrecord;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        Calendar cal = Calendar.getInstance();
        startDate = Calendar.getInstance();
        endDate = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);
        month = (cal.get(Calendar.MONTH));
        day_of_month = cal.get(Calendar.DAY_OF_MONTH);
        hour = cal.get(Calendar.HOUR_OF_DAY);
        minute = cal.get(Calendar.MINUTE);
        Date d = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        dateNowStr = sdf.format(d);
        tvYear.setText(getTime(d));
        tvMonth.setText(getTime1(d));
        cashList(page);

        outboundrecordadapter = new BaseQuickAdapter<StorageOutBean.DataBean, BaseViewHolder>(R.layout.item_outboundrecord, dataBeanList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, StorageOutBean.DataBean bean) {
                baseViewHolder.setText(R.id.tv_type, bean.getShops());
                baseViewHolder.setText(R.id.tv_date, bean.getCreate_time());
                baseViewHolder.setText(R.id.tv_money, bean.getWeights());
            }
        };
        outboundrecordadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                startActivity(new Intent(OutboundRecordActivity.this,StorageOutDetailsActivity.class).putExtra("id",dataBeanList.get(position).getId()+""));

            }
        });
        View emptyview = View.inflate(this, R.layout.empty_emptylayout, null);
        outboundrecordadapter.setEmptyView(emptyview);
        rvOutboundrecord.setAdapter(outboundrecordadapter);
        layoutRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                page = 1;
                cashList(page);


                refreshLayout.finishRefreshing();

            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                refreshLayout.finishLoadmore();
//                if (!TextUtils.isEmpty(goods_classify_id)) {


                page++;
                if (page < total) {
                    cashListMore(page);
                } else {
                    refreshLayout.setEnableLoadmore(false);

                }


//         }
            }
        });

    }

    private void cashListMore(int page) {
        EasyHttp.post(this)
                .api(new StorageOutListApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, "")).setPage(page + "").setTimer(dateNowStr)).request(new HttpCallback<StorageOutBean>(this) {

            @Override
            public void onSucceed(StorageOutBean data) {
                if (data.getCode() == 200) {
                    total = data.getCounts();

                    dataBeanList.addAll(data.getData());
                    outboundrecordadapter.setNewData(dataBeanList);
                    outboundrecordadapter.notifyDataSetChanged();


                } else {
                    toast(data.getMsg());
                }
            }


            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void cashList(int page) {
        EasyHttp.post(this)
                .api(new StorageOutListApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, "")).setPage(page + "").setTimer(dateNowStr)).request(new HttpCallback<StorageOutBean>(this) {

            @Override
            public void onSucceed(StorageOutBean data) {
                if (data.getCode() == 200) {
                    total = data.getCounts();
                    dataBeanList.clear();
                    dataBeanList.addAll(data.getData());
                    outboundrecordadapter.setNewData(dataBeanList);
                    outboundrecordadapter.notifyDataSetChanged();


                } else {
                    toast(data.getMsg());
                }
            }


            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        return format.format(date);
    }

    private String getTime1(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("MM");
        return format.format(date);
    }

    @OnClick(R.id.ll_date)
    public void onClick() {
        Calendar cal = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = (cal.get(Calendar.MONTH));
        int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        startDate.set(2020, 0, day_of_month, hour, minute);
        endDate.set(year, 11, 31, 23, 59);

        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                Log.e("TAG", "onTimeSelect: " + getTime(date) + "");
                tvYear.setText(getTime(date));
                tvMonth.setText(getTime1(date));
                dateNowStr = getTime(date) + "-" + getTime1(date);
                cashList(page);
            }
        }).setSubmitColor(getResources().getColor(R.color.color01B467))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.color333333))
                .setRangDate(startDate, endDate).setType(new boolean[]{true, true, false, false, false, false}).build();
        pvTime.show();
    }
}
